﻿using Jn698._45Tool.Service;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WebFirst.Entities;

namespace Communication
{
    internal class AnalysisMessage
    {
        public static int analysisPoint = 0;
        private static EnumInfoService enumInfoService = new EnumInfoService();
        /// <summary>
        /// 获取698有效帧
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public static byte[] GetValidFrameByte(byte[] frame)
        {
            if (frame == null)
            {
                return null;
            }
            int len = frame.Length;
            byte[] recivedata = new byte[len];
            int rcvdatalen = 0;
            int step = 0;
            try
            {
                for (int i = 0; i < len; i++)
                {
                    if (frame[i] == 0x68)
                    {
                        if (step == 0)
                        {
                            recivedata[rcvdatalen++] = frame[i];
                            step = 1;
                            continue;
                        }

                    }
                    else if (frame[i] == 0x16)
                    {
                        if (step == 1)
                        {
                            recivedata[rcvdatalen++] = frame[i];
                            short frameLen = BitConverter.ToInt16(new byte[] { recivedata[1], recivedata[2] }, 0);
                            if (frameLen == rcvdatalen - 2)
                            {
                                step = 2;
                                break;
                            }

                            continue;
                        }
                    }
                    else if (step == 1)
                    {
                        recivedata[rcvdatalen++] = frame[i];
                    }
                }
                if (step == 2)
                {
                    return recivedata;
                }
            }
            catch (Exception e)
            {

                return null;
            }

            return null;
        }

        public static string Analysis()
        {
            StringBuilder sb = new StringBuilder();

            return sb.ToString();
        }
        public static string AnalysisAPDU(byte[] frame)
        {
            analysisPoint = 14;
            if (analysisPoint >= frame.Length)
            {
                return null;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("--------解析--------\r\n");
            switch (frame[analysisPoint++])
            {
                //读取服务
                case 0x85:
                    switch (frame[analysisPoint++])
                    {
                        case 1:
                            sb.Append("读取一个对象属性的响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2")+"\r\n");
                            sb.Append(Analysis_GetRequestNormal(frame));
                            break;
                        case 2:
                            sb.Append("读取若干个对象属性的响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString() + "\r\n");
                            int num = frame[analysisPoint++];
                            sb.Append($"共有{num}个对象属性及其数据\r\n");
                            for (int i = 0;i< num; i++)
                            {
                                sb.Append($"第{i+1}个对象属性及其数据\r\n");
                                sb.Append(Analysis_GetRequestNormal(frame));
                            }
                            
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            if (frame[analysisPoint++] == 0)
            {
                sb.Append("无跟随上报信息域\r\n");
            }
            else
            {
                sb.Append("有跟随上报信息域\r\n");
            }
            if (frame[analysisPoint++] == 0)
            {
                sb.Append("没有时间标签\r\n");
            }
            else
            {
                sb.Append("有时间标签\r\n");
            }
            return sb.ToString();
        }
        public static string AnalysisData(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            int dataNum;
            switch (frame[analysisPoint++])
            {
                case 1:

                    dataNum = frame[analysisPoint++];
                    for (int i = 0; i < dataNum; i++)
                    {
                        //switch()
                        if (i==0)
                        {
                            sb.Append("总：");
                        }
                        else if (i==1)
                        {
                            sb.Append("费率1：");
                        }
                        else if (i == 2)
                        {
                            sb.Append("费率2：");
                        }
                        else if (i == 3)
                        {
                            sb.Append("费率3：");
                        }
                        else if (i == 4)
                        {
                            sb.Append("费率4：");
                        }
                        sb.Append(AnalysisData(frame) + "\r\n");
                    }
                    break;
                case 5:
                case 6:
                    int data = BitConverter.ToInt32(new byte[] { frame[analysisPoint] , frame[analysisPoint+1], frame[analysisPoint+2], frame[analysisPoint+3] }, 0);
                    analysisPoint += 4;
                    sb.Append(data.ToString());
                    break;
                case 0x59:
                    string s = frame[analysisPoint++].ToString("X2");
                    var d = Convert.ToSByte(s, 16);
                    sb.Append("换算："+ d.ToString()+"\r\n");
                    sb.Append("单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)) + "\r\n");
                    break;
                default:
                    break;
            }
            return sb.ToString();
        }
        private static string Analysis_GetRequestNormal(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            //OAD
            sb.Append("对象属性描述符：");
            string OI = "";
            int attributeID = 0;
            int index = 0;
            for (int i = 0; i < 4; i++)
            {
                if (i<2)
                {
                    OI += frame[analysisPoint].ToString("X2");
                }
                else if(i==2)
                {
                    attributeID = frame[analysisPoint];
                }
                else
                {
                    index = frame[analysisPoint];
                }
                sb.Append(frame[analysisPoint].ToString("X2"));
                analysisPoint++;
            }

            //SqlSugarHelper.Db.Queryable<ObjectID>().Where(o => o._ObjectID == OI);
            var l = SqlSugarHelper.Db.Queryable<ClassAttribute>().Where(c => c.MAttribute == attributeID).Select(c => c.MName).ToList();
            sb.Append("——" + l[0] + "\r\n\r\n其结果:\r\n");
            //Get-Result∷=CHOICE
            if (frame[analysisPoint++]==0)
            {
                switch (frame[analysisPoint++])
                {
                    case 6:
                        sb.Append("其结果:错误信息-对象不存在        6\r\n");

                        break;
                    default:
                        break;
                }
            }
            else
            {
                sb.Append(AnalysisData(frame));
            }
            
            return sb.ToString();
        }
    }
}
